<?php
class control_cate extends core_action{
	
	function __construct(){
		if($this->isAdminLogin() === false){
			$this->redirict(Qtpl::createUrl('index', 'login',array(),'admin'));
		}
	}
	
	function index(){
		$model = new core_model_cate();
		$rs = $model->select()->items;
		$cArr = array();
		$pArr = array();
		if($rs){
			foreach ($rs as $k=>$v){
				if($v['pid'] != 0){
					$cArr[$v['pid']][] = $v;
				}else{
					$pArr[$v['cid']] = $v;
				}
			}
		}
		$params['p'] = $pArr;
		$params['c'] = $cArr;
		$params['cate_cur'] = 'current';
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/cate_list.php', $params);
	}
	
	function show(){
		$cid = intval($_REQUEST['cid']);
		if($cid > 0){
			$mCate = new core_model_cate($cid);
			$params['cate'] = $mCate->get();
			$msg = "编辑";
			$m = 'edit';
		}else{
			$mCate = new core_model_cate();
			$msg = "添加";
			$m = 'create';
		}
		if($_POST){
			$post = Qutil::filter($_POST);
			if($post['token'] != md5($_COOKIE['adminKey'])){
				$this->showMsg("非法请求",2);
			}
			if($post['pid']>0 && $post['pid'] == $cid){
				$this->showMsg("分类参数错误",2);
			}
			if($mCate->$m($post) === false){
				$this->showMsg($msg."出错",2);
			}
			$model = new core_model_adminlog();
			$model->addLog(4, "{$msg}了分类：“{$mCate->getData('cate_name')}”");
			$this->showMsg($msg."成功",1,Qtpl::createUrl('cate', 'index','','admin'));
		}
		$model = new core_model_cate();
		$params['p'] = $model->select("pid=0")->items;
		$params['hname'] = $msg;
		$params['cate_cur'] = 'current';
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/cate.php', $params);
	}
	
	function del(){
		$cid = intval($_REQUEST['cid']);
		if($cid <=0 ) {
			$this->showMsg("删除出错",2);
		}
		if($_GET['token'] != md5($_COOKIE['adminKey'])){
			$this->showMsg("非法请求",2);
		}
		$model = new core_model_cate();
		$rs = $model->selectOne("pid={$cid}");
		if($rs){
			$this->showMsg("此类型下面还有内容无法删除",2);
		}
		$model = new core_model_bug();
		$rs = $model->selectOne("cate_id={$cid}");
		if($rs){
			$this->showMsg("此类型下面已经有发布漏洞无法删除",2);
		}
		$mCate = new core_model_cate($cid);
		$n = $mCate->getData('cate_name');
		$mCate->del();
		$model = new core_model_adminlog();
		$model->addLog(4, "删除了分类：“{$n}”");
		$this->showMsg("删除成功",1,Qtpl::createUrl('cate', 'index','','admin'));
	}
}